package com.novunda.platform.common.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * . 功能说明：创建pdf工具类
 *
 * @author FJ
 * @version 1.0
 * @see 2015-6-4
 */
public final class CreatePdfUtil {

    private static final Logger LOG = LoggerFactory.getLogger(CreatePdfUtil.class);

    /**
     * . 私有构造
     */
    private CreatePdfUtil() {

    }

    /**
     * . 表列字段字体
     */
    private static Font keyfont;
    /**
     * . 列值字体
     */
    private static Font textfont;

    static {
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
            textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
        } catch (DocumentException | IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }

    /**
     * . 功能说明：创建标题行
     *
     * @param title 标题
     * @return 标题行
     */
    public static PdfPCell createTitleCell(String title) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(title, keyfont));
        return cell;
    }

    public static PdfPCell createContentCell(String content, int index) {
        PdfPCell cell = new PdfPCell();
        String str = "";
        int length = content.length();
        int count = length / index;
        if (count > 1) {
            int lastIndex = 0;
            for (int i = 1; i <= content.length(); i++) {
                if (i % index == 0) {
                    str += content.substring(i - index, i) + "\n";
                    lastIndex = i;
                }
            }
            str += content.substring(lastIndex, content.length());
        } else if (count == 1) {
            str += content.substring(0, index) + "\n" + content.substring(index, length);
        } else {
            str = content;
        }
        cell.setPhrase(new Phrase(str, textfont));
        return cell;
    }

    /**
     * . 功能说明：创建一行无表格的标题
     *
     * @param value     标题
     * @param colspan   要何必的列
     * @param boderFlag 是否要边框
     * @return 一行对象
     */
    public static PdfPCell createCell(String value, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, keyfont));
        cell.setPadding(3.0f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        }
        return cell;
    }

    /**
     * . 功能说明：创建一个有主题和标题的table
     *
     * @param colNumber 字段数量
     * @param them      主题
     * @param titleList 标题集合
     * @return 创建的table
     */
    public static PdfPTable createTable(final float[] widths, final String them,
                                        final List<String> titleList) {
        PdfPTable table = new PdfPTable(widths);
    /*
    . 表格最大宽度
   */
        int maxWidth = 560;
        table.setTotalWidth(maxWidth);
        table.setLockedWidth(true);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);

        table.addCell(createCell(them, widths.length, false));
        for (String title : titleList) {
            table.addCell(createTitleCell(title));
        }
        return table;
    }

    /**
     * . 功能说明：无数据行
     *
     * @param table     表对象
     * @param colNumber 列数
     * @param time      时间
     * @return 表对象
     */
    public static PdfPTable noDataCell(final PdfPTable table,
                                       final int colNumber, final Date time) {
        for (int i = 0; i < colNumber - 1; i++) {
            table.addCell(createTitleCell("无数据"));
        }
//    table.addCell(DateUtil.getFormatDate(time));
        return table;
    }

    /**
     * . 功能说明：创建文档
     *
     * @param table      表格1
     * @param tableTwo   表格2
     * @param floderPath 文件夹路径
     * @param fileName   文件名称
     * @throws IOException       io异常
     * @throws DocumentException 文档异常
     */
    public static File createDocument(final PdfPTable table,
                                      final String floderPath, final String fileName) throws IOException,
            DocumentException {
        // 建立一个Document对象
        Document document = new Document();
        // 设置页面大小
        document.setPageSize(PageSize.A4);
        File file = new File(fileName);
        file.createNewFile();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        document.add(table);
        document.close();
        return file;
    }

}
